package test.zuche;

public class Business {
    private String name, dizhi;
    private String phone;
    private Car[] car = new Car[3];
    private String[] chepai = new String[50];
    private String[] jieday = new String[50];
    private String[] kaiday = new String[50];
    private int[] day = new int[50];
    private double[] money = new double[50];
    private int count = 0;//车辆
    private int index = 0;//租的人

    public Business() {
        this.name = "花园道租车公司";
        this.dizhi = "花园道";
        this.phone = "1869562147";
    }

    public void infor() {
        System.out.println("公司名" + name);
        System.out.println("公司地址" + dizhi);
        System.out.println("电话" + phone);
    }

    ////判断车牌存在
    public Car panpai(String chepai) {
        for (int i = 0; i < count; i++) {
            if (car[i].getChepai().equals(chepai)) {
                return car[i];
            }
        }
        return null;
    }

    public Car panduan(String chepai) {//判断是哪个车，是否可以出租
        Car a = this.panpai(chepai);
        if (a != null) {
            if (a.isChuzu()) {
                System.out.println("可以租车");
                System.out.print("你租的车为：");
                a.infor();
                return a;
            } else System.out.println("此车已经出租");
        }

        return null;
    }

    public double zu(int day, String chepai) {//租车并返回钱
        Car a = this.panpai(chepai);
        if (a != null) {
            a.setChuzu(false);
            return a.money(day);
        }
        return 0;
    }

    public void setJilu(int day, String chepai, String kaiday, String jieday, double money) {//接收租车人信息
        this.day[index] = day;
        this.chepai[index] = chepai;
        this.kaiday[index] = kaiday;
        this.jieday[index] = jieday;
        this.money[index] = money;
        index++;
    }

    public void zuchejilu() {
        if (index == 0) {
            System.out.println("暂无租车记录");
            return;
        }
        for (int i = 0; i < index; i++) {
            Car a = this.panpai(chepai[i]);
            a.infor();
            System.out.print("租的天数" + day[i] + "\t");
            System.out.print("租的开始时间" + kaiday[i] + "\t");
            System.out.print("租的结束时间" + jieday[i] + "\t");
            System.out.println("租金为：" + money[i]);
        }
    }

    public void huan(String chepai) {//还车
        Car a = this.panpai(chepai);
        if (a != null) {
            a.setChuzu(true);
            System.out.println("还车成功");
            return;
        }

        System.out.println("没有这个车的信息");
    }


    public void add(Car newcar) {
        Car a = this.panpai(newcar.getChepai());
        if(a!=null){
            System.out.println("添加失败，已存在此车牌");
            return;
        }
//        for (int i = 0; i < count; i++) {
//            if (car[i].getChepai().equals(newcar.getChepai())) {
//                System.out.println("添加失败，已存在此车牌");
//                return;
//            }
//        }
        //数组扩容
        if(count>=car.length){
            System.out.println("数组需要扩容！");
            Car[] temp=new Car[car.length+5];//新建一个更长的数组
            //把老的数组中的值挪过去
            for(int i=0;i<car.length;i++){
                temp[i]=car[i];
            }
            //把局部变量的temp的引用赋给car
            car=temp;//！！！
        }
        car[count++] = newcar;
    }
    //报废
    public void scrapped(String chepai) {
        for (int i = 0; i < count; i++) {
            if (car[i].getChepai().equals(chepai)) {
                for (int j = i; j < count - 1; j++) {
                    car[j] = car[j + 1];
                }
                car[--count] = null;
                System.out.println("车辆报废成功");

                return;
            }
        }
        System.out.println("没有这个车的信息");
    }

    public void inforche() {//遍历数组并输出
        if (count == 0) {
            System.out.println("没有车辆信息");
            return;
        }
        for (int i = 0; i < count; i++) {
            car[i].infor();
        }
    }

}
